<?php

namespace App\Admin\Controllers;

use App\Models\KeywordOne;
use App\Models\KeywordTwo;
use App\Models\MaterialUnit;
use App\Models\NaturalSpelling;
use App\Models\Paint;
use App\Models\TeachingMaterial;
use App\Models\TestBook;
use App\Models\Video;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MaterialUnitController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->title('教材单元列表')
            ->description('教材单元列表')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        Admin::style(
            <<<CSS
        table {
            text-align: center;
        }
CSS
        );
        return Grid::make(new MaterialUnit(), function (Grid $grid) {
            $grid->column('id');
            $grid->column('keyword_one_id');
            $grid->column('keyword_two_id');
            $grid->column('paint_id');
            $grid->column('natural_spelling_id');
            $grid->column('video_id');
            $grid->column('music_id');
            $grid->column('test_id');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MaterialUnit(), function (Show $show) {
            $show->field('id');
            $show->field('keyword_one_id');
            $show->field('keyword_two_id');
            $show->field('paint_id');
            $show->field('natural_spelling_id');
            $show->field('video_id');
            $show->field('music_id');
            $show->field('test_id');
        });
    }

    /**
     * Make a form builder.
     *
     * @param int $id
     * @return Form
     */
    protected function form(int $id = 0)
    {
        return Form::make(new MaterialUnit(), function (Form $form) use ($id) {
            $user = Admin::user();
            if ($user->is_admin==1) {
                $where = [];
            } else {
                $where = ['mechanism_id'=>$user->id];
            }
            $form->display('id');

            $form->select('keyword_one_id', '词汇一')
                ->options(KeywordOne::where($where)->pluck('name', 'id')->toArray())
                ->required();
            $form->select('keyword_two_id', '词汇二')
                ->options(KeywordTwo::where($where)->pluck('name', 'id')->toArray())
                ->required();
            $form->select('natural_spelling_id')
                ->options(NaturalSpelling::where($where)->pluck('name', 'id')->toArray())
                ->required();
            $form->select('paint_id')
                ->options(Paint::where($where)->pluck('name', 'id')->toArray())
                ->required();
            $form->select('video_id', '视频')
                ->options(Video::where($where)->pluck('name', 'id')->toArray())
                ->required();
            $form->select('music_id')
                ->options([0=>'无'])
                ->required();
            $form->select('test_id')
                ->options(TestBook::where($where)->pluck('name', 'id')->toArray())
                ->required();
            $form->hidden('book_id')->default($id);
            $form->disableViewButton();
            $form->disableResetButton();
            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->disableCreatingCheck();
        });
    }

    public function delete($book, $id)
    {
        return $this->form()->destroy($id);
    }

    public function newCreate(Content $content, TeachingMaterial $book)
    {
        if (Admin::user()->is_admin == -1 && $book->mechanism_id != Admin::user()->id) {
            abort(404);
        }
        return $content
            ->title($this->title())
            ->description($this->description()['create'] ?? trans('admin.create'))
            ->body($this->form($book->id));
    }

    public function newEdit($book, $id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description()['edit'] ?? trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    public function newUpdate($book, $id)
    {
        return $this->form()->update($id);
    }

    public function newShow($book, $id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description()['show'] ?? trans('admin.show'))
            ->body($this->detail($id));
    }

}
